#pragma once
#include <iostream>
#include <string>
#include <ctemplate/template.h>

#include "./oj_model.hpp"

namespace yx
{
    const std::string template_html_path = "./template_html/";

    class View
    {
    public:
        View() = default;
        ~View() = default;

        void allExpandHtml(const std::vector<struct Question> &all, std::string &html)
        {
            // 题目的编号 题目的标题 题目的难度 --- 使用表格显示
            // 1. 形成路径
            std::string src_html = template_html_path + "all_questions.html";
            // 2. 形成数字典
            ctemplate::TemplateDictionary root("all_questions");
            for (const auto &q : all)
            {
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("questions_table");
                sub->SetValue("number", q._number);
                sub->SetValue("title", q._title);
                sub->SetValue("difficulty", q._difficulty);
            }

            // 3. 获取被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            // 4. 开始完成渲染功能
            tpl->Expand(&html, &root);
        }
        void oneExpandHtml(const struct Question &q, std::string &html)
        {
            // 1. 形成路径
            std::string src_html = template_html_path + "one_question.html";

            // 2. 形成数字典
            ctemplate::TemplateDictionary root("one_question");
            root.SetValue("number", q._number);
            root.SetValue("title", q._title);
            root.SetValue("difficulty", q._difficulty);
            root.SetValue("desc", q._desc);
            root.SetValue("pre_code", q._head);

            // 3. 获取被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            // 4. 开始完成渲染功能
            tpl->Expand(&html, &root);
        }
    };
}